/*
 * Created on 2005-10-7
 * Last modified on 2009-11-7
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author HongGuang
 */
public class ParamUtils {

	private static final Log log = LogFactory.getLog(ParamUtils.class);

	public static final long LONG_ZERO = 0;
	public static final Integer INTEGER_ZERO = 0;
	public static final int INT_ZERO = INTEGER_ZERO.intValue();
	public static final short SHORT_ZERO = 0;

	public static final String STRING_EMPTY = StringHelper.EMPTY;

	public static void setAttribute(final HttpServletRequest request,
			String name, Object obj) {
		request.setAttribute(name, obj);
	}

	public static String getAttributeToString(final HttpServletRequest request,
			final String name, final String def) {
		if (ValidatorHelper.isEmpty(request)
				|| request.getAttribute(name) == null) {
			log.warn("...... #getAttributeToInteger(...) request is null"
					+ " OR request.getAttribute(name) is null");
			return def;
		}
		return String.valueOf(request.getAttribute(name));
	}

	public static Integer getAttributeToInteger(
			final HttpServletRequest request, final String name,
			final Integer def) {
		String str = getAttributeToString(request, name, null);
		return TypeChange.stringToInteger(str, def);
	}

	public static Long getAttributeToLong(final HttpServletRequest request,
			final String name, final Long def) {
		String str = getAttributeToString(request, name, null);
		return TypeChange.stringToLong(str, def);
	}

	public static Object getAttribute(HttpServletRequest request, String name) {
		return getAttribute(request, name, null);
	}

	public static Object getAttribute(HttpServletRequest request, String name,
			Object def) {
		if (ValidatorHelper.isEmpty(request)
				|| request.getAttribute(name) == null) {
			return def;
		}
		return request.getAttribute(name);
	}

	public static String getStringParameter(HttpServletRequest request,
			String name) {
		return getStringParameter(request, name, STRING_EMPTY);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultStr
	 * @return
	 */
	public static String getStringParameter(HttpServletRequest request,
			String name, String defaultStr) {
		if (!ValidatorHelper.isEmpty(request) && name != null
				&& !ValidatorHelper.isEmpty(request.getParameter(name))) {
			return TypeChange.ifNullToEmpty(request.getParameter(name)).trim();
		} else {
			return defaultStr;
		}
	}

	public static <Obj> Enumeration<Obj> getParameterNames(
			HttpServletRequest request) {
		if (!ValidatorHelper.isEmpty(request)) {
			return request.getParameterNames();
		} else {
			return null;
		}
	}

	public static String[] getParameterValues(HttpServletRequest request,
			String name) {
		if (!ValidatorHelper.isEmpty(request)) {
			return request.getParameterValues(name);
		} else {
			return null;
		}
	}

	public static String getRequestURL(HttpServletRequest request,
			String defaultStr) {
		String url = request.getScheme() + "://";
		url += request.getHeader("host");
		url += request.getRequestURI();
		if (request.getQueryString() != null)
			url += "?" + request.getQueryString();
		return url;
	}

	public static String getRequestURI(HttpServletRequest request,
			String defaultStr) {
		if (!ValidatorHelper.isEmpty(request)) {
			return request.getRequestURI();
		} else {
			return defaultStr;
		}
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultStr
	 * @return
	 */
	public static String getString(HttpServletRequest request, String name,
			String defaultStr) {
		if (ValidatorHelper.isEmpty(request) || ValidatorHelper.isEmpty(name)) {
			return defaultStr;
		}
		if (ValidatorHelper.isEmpty(request.getAttribute(name))) {
			if (ValidatorHelper.isEmpty(request.getParameter(name))) {
				return defaultStr;
			} else {
				return request.getParameter(name);
			}
		} else {
			return String.valueOf(request.getAttribute(name));
		}
	}

	public static int getInt(HttpServletRequest request, String name) {
		return getInt(request, name, INTEGER_ZERO);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultValue
	 * @return int
	 */
	public static int getInt(HttpServletRequest request, String name,
			int defaultValue) {
		String str = ParamUtils.getString(request, name, null);
		return TypeChange.stringToInt(str, defaultValue);
	}

	public static Long getLong(HttpServletRequest request, String name,
			Long defaultValue) {
		String str = ParamUtils.getString(request, name, null);
		return TypeChange.stringToLong(str, defaultValue);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultValue
	 * @return int
	 */
	public static Integer getInteger(HttpServletRequest request, String name,
			Integer defaultValue) {
		return TypeChange.stringToInteger(getString(request, name, null),
				defaultValue);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static int getIntParameter(HttpServletRequest request, String name) {
		return getIntParameter(request, name, INTEGER_ZERO);
	}

	public static Integer getIntegerParameter(HttpServletRequest request,
			String name, Integer def) {
		return TypeChange.stringToInteger(
				ParamUtils.getStringParameter(request, name, null), def);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultInt
	 * @return
	 */
	public static int getIntParameter(HttpServletRequest request, String name,
			int def) {
		return TypeChange.stringToInt(
				ParamUtils.getStringParameter(request, name), def);
	}

	public static double getDoubleParameter(HttpServletRequest request,
			String name, double def) {
		return TypeChange.stringToDouble(
				ParamUtils.getStringParameter(request, name), def);
	}

	public static double getDoubleParameter(HttpServletRequest request,
			String name) {
		return TypeChange.stringToDouble(ParamUtils.getStringParameter(request,
				name, "0"));
	}

	public static boolean getBooleanParameter(HttpServletRequest request,
			String name) {
		return TypeChange.stringToBoolean(ParamUtils.getStringParameter(
				request, name, "false"));
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param b
	 * @return
	 */
	public static Boolean getBooleanParameter(HttpServletRequest request,
			String name, Boolean b) {
		return TypeChange.stringToBoolean(
				ParamUtils.getStringParameter(request, name, null), b);
	}

	public static Boolean getBoolean(HttpServletRequest request, String name,
			Boolean b) {
		return TypeChange.stringToBoolean(
				ParamUtils.getString(request, name, null), b);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static Long getLongParameter(HttpServletRequest request, String name) {
		return TypeChange.stringToLong(ParamUtils.getStringParameter(request,
				name, null));
	}

	public static Long getLongParameter(HttpServletRequest request,
			String name, Long l) {
		return TypeChange.stringToLong(
				ParamUtils.getStringParameter(request, name, null), l);
	}

	public static long getLongParameter(HttpServletRequest request,
			String name, long l) {
		return TypeChange.stringToLong(
				ParamUtils.getStringParameter(request, name, null), l);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static byte getByteParameter(HttpServletRequest request, String name) {
		return TypeChange.stringToByte(ParamUtils.getStringParameter(request,
				name, null));
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param bnull
	 * @return
	 */
	public static Byte getByteParameter(HttpServletRequest request,
			String name, Byte def) {
		return TypeChange.stringToByte(
				ParamUtils.getStringParameter(request, name, null), def);
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static Short getShortParameter(HttpServletRequest request,
			String name) {
		return TypeChange.stringToShort(ParamUtils.getStringParameter(request,
				name, null));
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultshort
	 * @return
	 */
	public static Short getShortParameter(HttpServletRequest request,
			String name, Short defaultshort) {
		return TypeChange.stringToShort(
				ParamUtils.getStringParameter(request, name, null),
				defaultshort);
	}

}
